/*
https://leetcode.cn/problems/find-k-closest-elements/description/
658. 找到 K 个最接近的元素
medium, 李佳韵 2024.09.01
滑动窗口
*/

#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include<algorithm>
#include<vector>
using namespace std;
class Solution {
public:
    vector<int> findClosestElements(vector<int>& arr, int k, int x) {
        vector<int> result;
        vector<pair<int, int>> value;
        int n = arr.size();
        for (int i = 0; i < arr.size(); ++i) {
            value.push_back({ abs(arr[i] - x), arr[i] });
        }
        sort(value.begin(), value.end());
        for (int i = 0; i < k; ++i) {
            result.push_back(value[i].second);
        }
        sort(result.begin(), result.end());
        return result;
    }
};

int main() {
    Solution solution;
    vector<int> arr = { 1, 2, 3, 4, 5 };
    int k = 4;
    int x = 3;
    vector<int> c = solution.findClosestElements(arr, k, x);
    for (int num : c) {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}